﻿using System;
using System.Linq;
using System.Text;
using HslCommunication.BasicFramework;
using HslCommunication.Core;
using HslCommunication.Reflection;
using HslCommunication.Serial;

namespace HslCommunication.Profinet.FATEK
{
	/// <summary>
	/// 台湾永宏公司的编程口协议，具体的地址信息请查阅api文档信息，地址允许携带站号信息，例如：s=2;D100<br />
	/// The programming port protocol of Taiwan Yonghong company, 
	/// please refer to the api document for specific address information, The address can carry station number information, such as s=2;D100
	/// </summary>
	/// <remarks>
	/// <inheritdoc cref="T:HslCommunication.Profinet.FATEK.FatekProgramOverTcp" path="remarks" />
	/// </remarks>
	// Token: 0x020000AB RID: 171
	public class FatekProgram : SerialDeviceBase
	{
		/// <inheritdoc cref="M:HslCommunication.Profinet.FATEK.FatekProgramOverTcp.#ctor" />
		// Token: 0x06000C96 RID: 3222 RVA: 0x0004A7CA File Offset: 0x000489CA
		public FatekProgram()
		{
			base.ByteTransform = new RegularByteTransform();
			base.WordLength = 1;
		}

		/// <inheritdoc cref="P:HslCommunication.Profinet.FATEK.FatekProgramOverTcp.Station" />
		// Token: 0x170001EC RID: 492
		// (get) Token: 0x06000C97 RID: 3223 RVA: 0x0004A7EF File Offset: 0x000489EF
		// (set) Token: 0x06000C98 RID: 3224 RVA: 0x0004A7F7 File Offset: 0x000489F7
		public byte Station
		{
			get
			{
				return this.station;
			}
			set
			{
				this.station = value;
			}
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.FATEK.FatekProgramOverTcp.Read(System.String,System.UInt16)" />
		// Token: 0x06000C99 RID: 3225 RVA: 0x0004A800 File Offset: 0x00048A00
		[HslMqttApi("ReadByteArray", "")]
		public override OperateResult<byte[]> Read(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = FatekProgramOverTcp.BuildReadCommand(this.station, address, length, false);
			bool flag = !operateResult.IsSuccess;
			OperateResult<byte[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<byte[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<byte[]>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 2;
					if (flag3)
					{
						result = new OperateResult<byte[]>((int)operateResult2.Content[0], "Read Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						bool flag4 = operateResult2.Content[5] != 48;
						if (flag4)
						{
							result = new OperateResult<byte[]>((int)operateResult2.Content[5], FatekProgramOverTcp.GetErrorDescriptionFromCode((char)operateResult2.Content[5]));
						}
						else
						{
							byte[] array = new byte[(int)(length * 2)];
							for (int i = 0; i < array.Length / 2; i++)
							{
								ushort value = Convert.ToUInt16(Encoding.ASCII.GetString(operateResult2.Content, i * 4 + 6, 4), 16);
								BitConverter.GetBytes(value).CopyTo(array, i * 2);
							}
							result = OperateResult.CreateSuccessResult<byte[]>(array);
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.FATEK.FatekProgramOverTcp.Write(System.String,System.Byte[])" />
		// Token: 0x06000C9A RID: 3226 RVA: 0x0004A938 File Offset: 0x00048B38
		[HslMqttApi("WriteByteArray", "")]
		public override OperateResult Write(string address, byte[] value)
		{
			OperateResult<byte[]> operateResult = FatekProgramOverTcp.BuildWriteByteCommand(this.station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 2;
					if (flag3)
					{
						result = new OperateResult((int)operateResult2.Content[0], "Write Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						bool flag4 = operateResult2.Content[5] != 48;
						if (flag4)
						{
							result = new OperateResult<byte[]>((int)operateResult2.Content[5], FatekProgramOverTcp.GetErrorDescriptionFromCode((char)operateResult2.Content[5]));
						}
						else
						{
							result = OperateResult.CreateSuccessResult();
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.FATEK.FatekProgramOverTcp.ReadBool(System.String,System.UInt16)" />
		// Token: 0x06000C9B RID: 3227 RVA: 0x0004AA00 File Offset: 0x00048C00
		[HslMqttApi("ReadBoolArray", "")]
		public override OperateResult<bool[]> ReadBool(string address, ushort length)
		{
			OperateResult<byte[]> operateResult = FatekProgramOverTcp.BuildReadCommand(this.station, address, length, true);
			bool flag = !operateResult.IsSuccess;
			OperateResult<bool[]> result;
			if (flag)
			{
				result = OperateResult.CreateFailedResult<bool[]>(operateResult);
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = OperateResult.CreateFailedResult<bool[]>(operateResult2);
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 2;
					if (flag3)
					{
						result = new OperateResult<bool[]>((int)operateResult2.Content[0], "Read Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						bool flag4 = operateResult2.Content[5] != 48;
						if (flag4)
						{
							result = new OperateResult<bool[]>((int)operateResult2.Content[5], FatekProgramOverTcp.GetErrorDescriptionFromCode((char)operateResult2.Content[5]));
						}
						else
						{
							byte[] array = new byte[(int)length];
							Array.Copy(operateResult2.Content, 6, array, 0, (int)length);
							result = OperateResult.CreateSuccessResult<bool[]>(Enumerable.ToArray<bool>(Enumerable.Select<byte, bool>(array, (byte m) => m == 49)));
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc cref="M:HslCommunication.Profinet.FATEK.FatekProgramOverTcp.Write(System.String,System.Boolean[])" />
		// Token: 0x06000C9C RID: 3228 RVA: 0x0004AB1C File Offset: 0x00048D1C
		[HslMqttApi("WriteBoolArray", "")]
		public override OperateResult Write(string address, bool[] value)
		{
			OperateResult<byte[]> operateResult = FatekProgramOverTcp.BuildWriteBoolCommand(this.station, address, value);
			bool flag = !operateResult.IsSuccess;
			OperateResult result;
			if (flag)
			{
				result = operateResult;
			}
			else
			{
				OperateResult<byte[]> operateResult2 = base.ReadBase(operateResult.Content);
				bool flag2 = !operateResult2.IsSuccess;
				if (flag2)
				{
					result = operateResult2;
				}
				else
				{
					bool flag3 = operateResult2.Content[0] != 2;
					if (flag3)
					{
						result = new OperateResult((int)operateResult2.Content[0], "Write Faild:" + SoftBasic.ByteToHexString(operateResult2.Content, ' '));
					}
					else
					{
						bool flag4 = operateResult2.Content[5] != 48;
						if (flag4)
						{
							result = new OperateResult<bool[]>((int)operateResult2.Content[5], FatekProgramOverTcp.GetErrorDescriptionFromCode((char)operateResult2.Content[5]));
						}
						else
						{
							result = OperateResult.CreateSuccessResult();
						}
					}
				}
			}
			return result;
		}

		/// <inheritdoc />
		// Token: 0x06000C9D RID: 3229 RVA: 0x0004ABE2 File Offset: 0x00048DE2
		public override string ToString()
		{
			return string.Format("FatekProgram[{0}:{1}]", base.PortName, base.BaudRate);
		}

		// Token: 0x0400036B RID: 875
		private byte station = 1;
	}
}
